package com.tlias.service.impl;


import com.tlias.mapper.DeptMapper;
import com.tlias.pojo.Dept;

import com.tlias.pojo.Result;
import com.tlias.service.DeptService;
import com.tlias.utils.CommomUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    DeptMapper deptMapper;

    /**
     * 部门列表查询
     *
     * @return
     */
    @Override
    public Result getDeptListService() {
        List<Dept> depts = deptMapper.deptListSql();
        if (depts != null && depts.size() > 0) {
            return Result.success(depts);
        }
        if (depts != null && depts.size() == 0) {
            return Result.success("暂无数据");
        }
        return Result.error("系统异常");
    }

    /**
     * 增加部门
     *
     * @param name
     * @return
     */
    @Override
    public Result addDeptService(String name) {
        if (name.length() > 10 || name.length()<2) {
            return Result.error("部门名长度不得超过10或小于2");
        }
        //查询部门名是否重复
        Dept deptByNameSql = deptMapper.findDeptByNameSql(name);
        if (deptByNameSql != null) {
            return Result.error("部门名重复");     //部门已存在
        }
        //开始插入
        Dept dept = new Dept();
        dept.setName(name);
        //设置时间
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        Integer rs = deptMapper.addDeptSql(dept);
        if (rs == 1) {
            return Result.success();
        } else {
            //数据未插入成功
            log.error("service_addDept_Erro");
            return Result.error("系统异常");
        }
    }

    /**
     * 根据id查部门
     *
     * @param id
     * @return
     */
    @Override
    public Result FindDeptById(Integer id) {
        if (id == null) {
            return Result.error("id为空");
        }
        if (id < 1) {
            return Result.error("非法id");
        }
        //开始查询
        Dept dept = deptMapper.findDeptByIdSql(id);
        if (dept != null) {
            return Result.success(dept);
        } else {
            return Result.error("部门不存在");
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public Result delectDeptById(Integer id) {
        if (id == null) {
            return Result.error("id为空");
        }
        if (id < 0) {
            return Result.error("非法id");
        }
        Integer rs = deptMapper.deleteById(id);
        if (rs == 1) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }

    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @Override
    public Result updateDept(Dept dept) {
        if (dept.getId() == null) {
            return Result.error("id为空");
        }
        if (dept.getName() == null) {
            return Result.error("部门名为空");
        }
        if (dept.getName().length() < 2 || dept.getName().length() > 10) {
            return Result.error("部门名长度不得超过10或小于2");
        }
        //设置更新时间
        dept.setUpdateTime(LocalDateTime.now());
        //执行修改
        Integer rs = deptMapper.updateBYId(dept);
        if (rs == 1) {
            return Result.success();
        } else {
            return Result.error("修改失败");
        }

    }
}
